//
//  OilLevelVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/23.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "OilLevelVC.h"
#import "OilLevelCell.h"
#import "CoordinatesModel.h"
#import "DeviceOilLevelVC.h"
@interface OilLevelVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tbView;

@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation OilLevelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"油位";
    self.tbView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self.view addSubview:self.tbView];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"OilLevelCell" bundle:nil] forCellReuseIdentifier:@"OilLevelCell"];
    [self.tbView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    [self requestALLDeviceData];

}


#pragma mark tableViewDelegate &&tableViewDataSource
- (void)requestALLDeviceData{
    
    
    NSString *url = [QParamService urlWithIOTAction:@"map/deviceList"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[QParamService getToken] forKey:@"token"];
    [param setObject:@"0" forKey:@"pageNum"];
    [param setObject:@"0" forKey:@"pageSize"];
    [param setObject:@"-1" forKey:@"workStatus"];
    
    [[QHttpManager sharedManager] POST:url parameterMD5jm:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if (error) {
            
            NSLog(@"error:%@",basModel.errMsg);
            return ;
        }
        
        if([basModel.result isEqualToString:@"success"]){
            
            NSLog(@"dic:%@",basModel.items);
            NSArray *array = basModel.items;
            for (NSDictionary *dic in array) {
                
                CoordinatesModel *model = [CoordinatesModel objectFromDict:dic];
                
                [self.dataSource addObject:model];
                
            }
            
        }
        
        [self.tbView reloadData];
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 &&indexPath.row ==1 ) {
        return 10;
        
    }
    
    return 50;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 2;
        
    }
    
    return self.dataSource.count;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OilLevelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OilLevelCell"];
    
    if (indexPath.section == 0&&indexPath.row == 0) {
        
        return cell;
        
    }
    
    if (indexPath.section == 0&& indexPath.row == 1) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
        
        cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        return cell;
        
    }
    
    NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
    
    [cell setCellWithData:dic];

    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   CoordinatesModel *coorModel =  [self.dataSource objectAtIndex:indexPath.row];
   
    if (indexPath.section == 1) {
        
        DeviceOilLevelVC *vc = [[DeviceOilLevelVC alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
        
        vc.title = coorModel.deviceName;
        
        vc.deviceID = coorModel.ID;
        
        vc.oilLevel = coorModel.oilLevel;
        
    }
    
}

-(UITableView *)tbView{
    
    if (!_tbView) {
        
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _tbView.delegate = self;
        
        _tbView.dataSource = self;
        
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        _tbView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        
    }
    return _tbView;
    
}
ARRAY_INIT(dataSource);
@end
